package com.wsc.wisdomchina.activitity.minemodule;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.JsonTools;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchina.activitity.fourmodule.CompanyInfoDetailActivity;
import com.wsc.wisdomchina.activitity.fourmodule.FloorInfoDetailActivity;
import com.wsc.wisdomchina.adapter.WDFB_QYFB_Adapter;
import com.wsc.wisdomchina.constant.Constant;
import com.wsc.wisdomchina.javaBean.QiYeFaBuListBean;
import com.wsc.wisdomchina.javaBean.RespObj;
import com.wsc.wisdomchina.presenter.ContributorPresenter;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author jaily.zhang
 * @version V1.3.1
 * @Description 我的发布
 * @date 2015-12-28 下午2:08:43
 */
public class MyFaBuActivity extends BaseActivity implements OnClickListener {

    private PullToRefreshListView lv_today_information;
    private ListView actualListView;
    private Context mcontext;
    private int skip = 0;
    private final int limit = 6;
    private ContributorPresenter contributorPresenter;
    private final String HOME_NEWS = MyFaBuActivity.class.getSimpleName();
    private List<QiYeFaBuListBean> homeNewsList;
    private WDFB_QYFB_Adapter quanBuAdapter;
    private boolean isfresh = true;
    private ProgressBar pb_todayInfo;
    private ImageView regist_back;
    private TextView tv_headtitle;
    private RelativeLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myfabu_layout);
        mcontext = this;
        contributorPresenter = new ContributorPresenter(this, mcontext);
        initView();
        showData();
    }

    /**
     * @Description 初始化View控件
     * @author jaily.zhang
     */
    private void initView() {

        lv_today_information = (PullToRefreshListView) findViewById(R.id.lv_today_information);
        lv_today_information.setMode(PullToRefreshBase.Mode.BOTH);
        pb_todayInfo = (ProgressBar) findViewById(R.id.pb_todayInfo);
        actualListView = lv_today_information.getRefreshableView();
        layout=(RelativeLayout) findViewById(R.id.layout);
        actualListView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        Tools.showRefreshView(lv_today_information);

        regist_back = (ImageView) findViewById(R.id.regist_back);
        tv_headtitle = (TextView) findViewById(R.id.tv_headtitle);
        regist_back.setOnClickListener(this);
        tv_headtitle.setText("信息发布");

        lv_today_information.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {

            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                // TODO Auto-generated method stub
                // Update the LastUpdatedLabe
                // 处理下拉刷新的业务
                if (refreshView.getCurrentMode().equals(PullToRefreshBase.Mode.PULL_FROM_START)) {
                    isfresh = true;
                    Tools.showRefreshView(lv_today_information);
                    skip = 0;
                    showData();
                } else {
                    isfresh = false;
                    Tools.showRefreshView(lv_today_information);
                    skip += limit;
                    showData();
                }

            }
        });

        actualListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                QiYeFaBuListBean qiYeFaBuListBean = homeNewsList.get(position - 1);
                try {
                    //企业
                    if (qiYeFaBuListBean.getType() == 0) {
                        Intent intent = new Intent(mcontext, CompanyInfoDetailActivity.class);
                        intent.putExtra("id", homeNewsList.get(position - 1).getId());
                        intent.putExtra("classname", MyFaBuActivity.class.getSimpleName());
                        startActivityForResult(intent, 0);
                    }
                    //楼宇
                    else {
                        Intent intent = new Intent(mcontext, FloorInfoDetailActivity.class);
                        intent.putExtra("bid", homeNewsList.get(position - 1).getId());
                        intent.putExtra("classname", MyFaBuActivity.class.getSimpleName());
                        startActivityForResult(intent, 0);
                    }
                } catch (Exception e) {

                }

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0 && resultCode == 1) {
            isfresh=true;
            skip = 0;
            showData();
        }
    }

    /**
     * @Description 服务器获取数据
     * @author jaily.zhang
     */
    private void showData() {
        String uid = Tools.getSharedPreferencesStrings(Constant.SharePreferenceUserid, "", mcontext);
        //首页资讯
        contributorPresenter.getQiYeFaBu(uid, skip, limit, HOME_NEWS);
    }


    @Override
    public void onLoadContributorStart(String flag) {

    }


    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            if (flag.equals(HOME_NEWS)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code = respObj.getCode();
                Object object = respObj.getMessage();
                if (code.equals("200") && object != null) {
                    String jsonMessage = JsonTools.objectToJsonString(object);
                    List<QiYeFaBuListBean> homeNewsItem = JsonTools.jsonStringToList(jsonMessage, QiYeFaBuListBean.class);
                    if (homeNewsItem != null && homeNewsItem.size() > 0) {
                        actualListView.setVisibility(View.VISIBLE);
                        if (isfresh) {
                            layout.setVisibility(View.GONE);
                            homeNewsList = homeNewsItem;
                            quanBuAdapter = new WDFB_QYFB_Adapter(mcontext, homeNewsList);
                            actualListView.setAdapter(quanBuAdapter);
                        } else {
                            layout.setVisibility(View.GONE);
                            homeNewsList.addAll(homeNewsItem);
                            quanBuAdapter.notifyDataSetChanged();
                        }

                    } else {
                        if (isfresh) {
                            layout.setVisibility(View.VISIBLE);
                            actualListView.setVisibility(View.GONE);
                        }
                    }

                }
            }
        } catch (Exception e) {

        }
    }


    @Override
    public void onLoadContributorComplete(String flag) {
        lv_today_information.onRefreshComplete();
        pb_todayInfo.setVisibility(View.GONE);
    }

    @Override
    public void onLoadContributorError(String error, String flag) {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.regist_back:
                finish();
                break;
        }
    }
}
